package com.zzy.demo.common.util.rsa;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * 
 * @author guokaige
 * @date: 2018年11月21日 下午3:31:47
 */
public  class HutoolRsaUtil {
	
	public static String priKey="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIK2l62kOOFJQJaKonU2nyG3lrIHSfcp4Yu1wLgQsGYiKBxKM+1Xp93KSTIM2c359491nP20HvmPD8MGOJHSu/4HWEW+h8R2SKS+JO+9UBb+ZKGyojf28Fnb4L2VkpvXuFazKk5iCeGSid2SRKwqLZXPS8YZpxIT2gMnP5dyhzXPAgMBAAECgYBHA5ZSIxc35gkuAHGUhrFbqxc271jS7DD6KythlMeRJVtvSyz89BNiHqU3iN3H1tcVXIuHFhWB9aRViwquxrOU1OIxagkkCXGd+qZuaeENjqvBbr/uooCf2ZaQdFxb44VG5cXk5uTcBC5ZNQWNeEhaXxD6r+aCBA4My7UUiLTHmQJBAP0zUcbbTvW1sDKAZR+fTkw+fC9kxqDHHLpdSd+/SB3pv/83klF5c4Bwo9PSF59YZFG04SiLDXEhFGgWneUbRa0CQQCEKJMaJU7aDlyEpq8CcNGOU4AU20KvSVbxBWk0ybKmKad76SrFSzSM5rECTvx3wHW4fp2NnOC/DO85FshJv0DrAkAp4E37HJRfkPAWOiP/aeKSO5dgY0NyndvDeuBX8BPmVp2ZgUBi1zpiGL8PrmSXxw+4b7j5cp4DgtwUJwFwPeV1AkASMoQO1ZRF8jRLD6iJbX9tWZIJTSeheze9eEFEQ4GV36ZwKWr6P6C8xgcZika5Nob/IHART8iiheVqtvhdLziBAkARTH7aaEEOyynDFObpcj+z2Z7jmxUd/d0eOd/kIMJCYoK7zrosf/Wrt3/c+tvxFL/K3AJUXvYTtworqKCEnofF";
	public static String pubKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCCtpetpDjhSUCWiqJ1Np8ht5ayB0n3KeGLtcC4ELBmIigcSjPtV6fdykkyDNnN+fePdZz9tB75jw/DBjiR0rv+B1hFvofEdkikviTvvVAW/mShsqI39vBZ2+C9lZKb17hWsypOYgnhkondkkSsKi2Vz0vGGacSE9oDJz+Xcoc1zwIDAQAB";
	
	public static String keyword="woshi中国@.com";
	
	
	public static CmsKeyPair generateKeyPair() {
		KeyPair pair = SecureUtil.generateKeyPair("RSA");
		
		String privateKeyStr = null;
		String publicKeyStr = null;
		PrivateKey privateKey = null;
	    PublicKey publicKey = null;
		try {
			privateKey = pair.getPrivate();
			publicKey = pair.getPublic();
			privateKeyStr = ByteUtils.toHexString(privateKey.getEncoded());
			publicKeyStr = ByteUtils.toHexString(publicKey.getEncoded());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new CmsKeyPair(privateKey, publicKey, privateKeyStr, publicKeyStr);
	}
	
	
	

	public static String encryptByPublicKey(String publicKey, String content) throws UnsupportedEncodingException {
		byte[] pubKey = ByteUtils.fromHexString(publicKey);
		RSA rsa = new RSA(null,pubKey);
		String encoreStr = rsa.encryptHex(content, KeyType.PublicKey);
		return encoreStr;
	}
	
	public static String decryptByPrivateKey(String privateKey, String encoreStr) throws UnsupportedEncodingException {
		byte[] priKey = ByteUtils.fromHexString(privateKey);
		RSA rsa = new RSA(priKey,null);
		String content = rsa.decryptStr(encoreStr, KeyType.PrivateKey);
		return content;
	}


	
	
	
	public static String encryptByPrivateKey(String privateKey, String content) throws UnsupportedEncodingException {
		byte[] priKey = ByteUtils.fromHexString(privateKey);
		RSA rsa = new RSA(priKey,null);
		String encoreStr = rsa.encryptHex(content, KeyType.PrivateKey);
		return encoreStr;
	}
	
	public static String decryptByPublicKey(String publicKey, String encoreStr) throws UnsupportedEncodingException {
		byte[] pubKey = ByteUtils.fromHexString(publicKey);
		RSA rsa = new RSA(null,pubKey);
		String content = rsa.decryptStr(encoreStr, KeyType.PublicKey);
		return content;
	}
    
    public static void main(String[] args) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    	
    	CmsKeyPair cmsKeyPair = HutoolRsaUtil.generateKeyPair();
    	String priKey = cmsKeyPair.getPrivateKeyStr();
    	String pubKey = cmsKeyPair.getPublicKeyStr();
    	System.out.println("priKey:" + priKey);
    	System.out.println("pubKey:" + pubKey);

       	
    	String hexEncodeStr = HutoolRsaUtil.encryptByPublicKey(pubKey, keyword);
    	System.out.println("backBcdEncodeStr:"+hexEncodeStr);
    	
    	String content = HutoolRsaUtil.decryptByPrivateKey(priKey, hexEncodeStr);
    	System.out.println("content:"+content);

    	
	}
}